clear all
global system "linux"

if "${system}" == "linux" {
	*global code "/"
	global code "/"
	global s "/"
}

run "${code}${s}_set-path.do"

use "${cached}/embs-GSE-loanlevel-combined.dta", clear

** Sample Selection
keep if orig_term == 360
keep if num_units==1
keep if orig_ym >= ym(2009, 1)

** clean vars
qui do "${code}/_clean-embs-vars.do"
drop if orig_loan_amount == .
drop if orig_loan_to_value == .


**** Create shares of TBA ineligible pool
gen tbaelig = tbaeligcode_id == "Y"
gen jm_pool = inlist(embs_product, "FHLG30JM", "FNM30JM")
gen hiltv_pool = inlist(embs_product, "FHLG30HILTV", "FNM30HILTV")
gen other_inelig_pool = 1 - tbaelig - jm_pool - hiltv_pool

collapse tbaelig jm_pool hiltv_pool other_inelig_pool [aw=orig_loan_amount], by(orig_ym) fast

save "${temp}/tbaelig-share.dta", replace
export delimited "${temp}/tbaelig-share.csv", replace
